package com.doge.service.wx.topic;

import com.doge.bean.base.BaseData;
import com.doge.bean.pojo.goods.Goods;
import com.doge.bean.pojo.topic.Topic;
import com.doge.bean.pojo.topic.TopicExample;
import com.doge.bean.vo.topic.TopicReadVO;
import com.doge.mapper.GoodsMapper;
import com.doge.mapper.TopicMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: doge
 * @description: 主题的活动
 * @author: Mr.Jiang
 * @create: 2021-12-02 16:02
 **/
@Service
public class WxTopicServiceImpl implements WxTopicService{
    @Autowired
    TopicMapper topicMapper;
    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public BaseData list(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        TopicExample topicExample = new TopicExample();
        topicExample.setOrderByClause("add_time desc");
        topicExample.createCriteria().andDeletedEqualTo(false);
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        PageInfo pageInfo = new PageInfo(topics);
        BaseData baseData = new BaseData();
        baseData.setPages(pageInfo.getPages());
        baseData.setPage(page);
        baseData.setTotal(pageInfo.getTotal());
        baseData.setLimit(limit);
        baseData.setList(topics);
        return baseData;
    }

    @Override
    public BaseData related() {
        PageHelper.startPage(1,5);
        TopicExample topicExample = new TopicExample();
        topicExample.createCriteria().andDeletedEqualTo(false);
        List<Topic> list = topicMapper.selectByExample(topicExample);
        ArrayList<Topic> topics = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            topics.add(list.get(i));
        }
        PageInfo pageInfo = new PageInfo(list);
        BaseData baseData = new BaseData();
        baseData.setLimit(5);
        baseData.setTotal(pageInfo.getTotal());
        baseData.setPage(1);
        baseData.setPages(pageInfo.getPages());
        baseData.setList(topics);
        return baseData;
    }

    @Override
    public TopicReadVO detail(Integer id) {
        Topic topic = topicMapper.selectByPrimaryKey(id);
        //更具topic查询商品
        Integer[] ids = topic.getGoods();
        ArrayList<Goods> goods1 = new ArrayList<>();
        for (Integer integer : ids) {
            Goods goods = goodsMapper.selectByPrimaryKey(integer);
            goods1.add(goods);
        }
        TopicReadVO topicReadVO = new TopicReadVO();
        topicReadVO.setGoodsList(goods1);
        topicReadVO.setTopic(topic);
        return topicReadVO;
    }


}
